package com.lyf.core.ui.dialog;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.viewbinding.ViewBinding;

import com.blankj.utilcode.util.ToastUtils;
import com.lyf.core.presenter.BasePresenter;
import com.lyf.core.presenter.view.BaseView;
import com.lyf.core.ui.activity.BaseActivity;
import com.trello.rxlifecycle4.LifecycleProvider;
import com.trello.rxlifecycle4.LifecycleTransformer;
import com.trello.rxlifecycle4.RxLifecycle;
import com.trello.rxlifecycle4.android.ActivityEvent;
import com.trello.rxlifecycle4.android.RxLifecycleAndroid;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.subjects.BehaviorSubject;


public abstract class BaseMvpConterPopup<VB extends ViewBinding,T extends BasePresenter> extends BaseCenterPopup<VB> implements BaseView, LifecycleProvider<ActivityEvent> {

    private static final String TAG = "BaseMvpBottomPopup";

    private final BehaviorSubject<ActivityEvent> lifecycleSubject = BehaviorSubject.create();

    protected T mPresenter;

    public abstract T getPresenter();

    public BaseMvpConterPopup(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        lifecycleSubject.onNext(ActivityEvent.CREATE);
        mPresenter=getPresenter();
        if (mPresenter!=null){
            mPresenter.attachView(this);
            mPresenter.addObserver(RxLifecycle.bindUntilEvent(lifecycleSubject, ActivityEvent.DESTROY));
        }
    }
    @Override
    protected void doAfterShow() {
        super.doAfterShow();
        initData();
    }

    protected void initData(){

    }

    @Override
    public void destroy() {
        super.destroy();
        lifecycleSubject.onNext(ActivityEvent.DESTROY);
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }

    @Override
    public void showMessage(String message) {
        super.showMessage(message);
        ToastUtils.showShort(message);
    }

    @Override
    public void showMessage(int message) {
        super.showMessage(message);
        ToastUtils.showShort(message);
    }

    @Override
    public void showLoading() {
        if (getContext() instanceof BaseActivity){
            BaseActivity baseActivity= (BaseActivity) getContext();
            baseActivity.showLoading();
        }
    }

    @Override
    public void stopLoading() {
        if (getContext() instanceof BaseActivity){
            BaseActivity baseActivity= (BaseActivity) getContext();
            baseActivity.stopLoading();
        }
    }

    @Override
    public void showLoading(String title) {
        if (getContext() instanceof BaseActivity){
            BaseActivity baseActivity= (BaseActivity) getContext();
            baseActivity.showLoading(title);
        }
    }

    @Override
    public Observable<ActivityEvent> lifecycle() {
        return lifecycleSubject.hide();
    }

    @Override
    public <T> LifecycleTransformer<T> bindUntilEvent(ActivityEvent event) {
        return RxLifecycle.bindUntilEvent(lifecycleSubject, event);
    }

    @Override
    public <T> LifecycleTransformer<T> bindToLifecycle() {
        return RxLifecycleAndroid.bindActivity(lifecycleSubject);
    }
}
